home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 051-075 / disk_055 / palette / palette.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  14KB  |  524 lines

  1. /*
  2.  * TITLE: Palette.c --- Adjusts colors of front screen or custom test screen
  3.  *
  4.  *  Palette.c      C. Scheppner  CBM  10/86
  5.  *
  6.  *    Inspired by Charlie Heath's Palette.c
  7.  *
  8.  *    Opens a palette tool in the front or only screen.
  9.  *    Optional args open a test screen for the palette:
  10.  *    USAGE:  palette [bitplanes] [screentype]
  11.  *      screentype 1=320x200 2=320x400 3=640x200 4=640x400
  12.  */
  13.  
  14. #include <exec/types.h>
  15. #include <exec/memory.h>
  16. #include <intuition/intuition.h>
  17. #include <intuition/intuitionbase.h>
  18.  
  19. #define WIDE 320
  20. #define HIGH 200
  21. #define DEEP   5
  22.  
  23.  
  24. /* Screen for custom screen palette */
  25.  
  26. struct TextAttr topaz80 =
  27.    {"topaz.font",TOPAZ_EIGHTY,FS_NORMAL,FPF_ROMFONT };
  28.  
  29. struct NewScreen newtScreen =
  30.    { 0,0,WIDE,HIGH,DEEP,      /* Corner, Size         */
  31.      1,2,0,                   /* Detail, Block, Modes */
  32.      CUSTOMSCREEN,&topaz80,   /* ScreenType, Font     */
  33.      "Custom Screen",
  34.      0,0 };                   /* Gadgets, Bitmap      */
  35.  
  36.  
  37. /* Other globals of caller */
  38. struct IntuitionBase *IntuitionBase;
  39. struct GfxBase       *GfxBase;
  40. struct Screen        *pscreen, *tscreen;
  41. BOOL  FromCLI = TRUE;
  42.  
  43. /* Sample calling program */
  44.  
  45. main(argc,argv)
  46. int argc;
  47. char **argv;
  48.    {
  49.    USHORT testpl, testsz;
  50.  
  51.    if (!(IntuitionBase =
  52.          (struct IntuitionBase *)OpenLibrary("intuition.library",30)))
  53.             cleanexit("Can't open intuition\n");
  54.    if (!(GfxBase =
  55.          (struct GfxBase *)OpenLibrary("graphics.library",30)))
  56.             cleanexit("Can't open graphics\n");
  57.  
  58.    if (argc==0) FromCLI = FALSE;
  59.  
  60.    tscreen = NULL; /* Null so we won't close if we didn't need to open */
  61.    if (argc==3)    /* Arguments passed, open a test screen  */
  62.       {
  63.       testpl = ((USHORT)*argv[1])&0x0F;
  64.       testsz = ((USHORT)*argv[2])&0x0F;
  65.  
  66.       if ((testpl<1)||(testpl>5)) testpl = 5;
  67.       if (((testsz==2)||(testsz==4))&&(testpl==5))  testpl = 4;
  68.  
  69.       newtScreen.Depth = testpl;
  70.       if (testsz==2) newtScreen.Width=640,  newtScreen.ViewModes = HIRES;
  71.       if (testsz==3) newtScreen.Height=400, newtScreen.ViewModes = LACE;
  72.       if (testsz==4) newtScreen.Width=640,  newtScreen.Height = 400,
  73.                                          newtScreen.ViewModes = HIRES|LACE;
  74.       
  75.       if (!(tscreen=
  76.                (struct Screen *)OpenScreen(&newtScreen)))
  77.                    cleanexit("Can't open test screen\n");
  78.       pscreen = tscreen;
  79.       }
  80.    else   /* Open palette on the front screen */
  81.       {
  82.       pscreen = IntuitionBase->FirstScreen;
  83.       }
  84.  
  85.    if (!(doPalette(pscreen)))  cleanexit ("Can't open palette\n");
  86.    cleanexit("");
  87.    }
  88.  
  89.  
  90. cleanexit(s)
  91. char *s;
  92.    {
  93.    int error = 0;
  94.  
  95.    if ((*s)&&(FromCLI)) printf(s);
  96.    if (*s) error = 1;
  97.    if (tscreen)  CloseScreen(tscreen);    /* if test screen opened */
  98.    if (GfxBase) CloseLibrary(GfxBase);
  99.    if (IntuitionBase)  CloseLibrary(IntuitionBase);
  100.    exit(error);
  101.    }
  102.  
  103.  
  104. /* ================================================================ */
  105.  
  106. /* Globals for doPalette() */ 
  107.  
  108. #define MAXCOLORS 32
  109. #define RGB_BODY  0xFFF
  110.  
  111. #define PWIDE 192         /* Palette Window Width */
  112. #define PBASEHIGH 72      /* Height before adjust for color rows */
  113.  
  114. /* Gadget ID's */
  115. #define colID  1
  116. #define okID   MAXCOLORS + 1
  117. #define resID  MAXCOLORS + 2
  118. #define canID  MAXCOLORS + 3
  119. #define rID    MAXCOLORS + 4
  120. #define bID    MAXCOLORS + 5
  121. #define gID    MAXCOLORS + 6
  122.  
  123. /* Proportional RGB Gadgets */
  124.  
  125. #define PRPX  52
  126. #define PRPY -62  /* Top prop, relative to bottom */
  127. #define PRPW  90
  128. #define PRPH  11
  129.  
  130. struct IntuiText
  131.    rTxt = {1,0,JAM1,-11,2,0,"R",0},
  132.    gTxt = {1,0,JAM1,-11,2,0,"G",0},
  133.    bTxt = {1,0,JAM1,-11,2,0,"B",0};
  134.  
  135. struct Image  rImg, gImg, bImg;
  136.  
  137. struct PropInfo
  138.    rInf = {AUTOKNOB|FREEHORIZ,0,0,RGB_BODY,0,0,0,0,0,0,0},
  139.    gInf = {AUTOKNOB|FREEHORIZ,0,0,RGB_BODY,0,0,0,0,0,0,0},
  140.    bInf = {AUTOKNOB|FREEHORIZ,0,0,RGB_BODY,0,0,0,0,0,0,0};
  141.  
  142. struct Gadget
  143.    bGad = { NULL, PRPX,PRPY+30, PRPW,PRPH,
  144.             GADGHNONE|GADGIMAGE|GRELBOTTOM,
  145.             GADGIMMEDIATE|RELVERIFY|FOLLOWMOUSE,
  146.             PROPGADGET,(APTR)&bImg, 0,
  147.             &bTxt, 0,(APTR)&bInf, bID, 0 },   /* Last Gadget */
  148.  
  149.    gGad = { &bGad, PRPX,PRPY+15, PRPW,PRPH,
  150.             GADGHNONE|GADGIMAGE|GRELBOTTOM,
  151.             GADGIMMEDIATE|RELVERIFY|FOLLOWMOUSE,
  152.             PROPGADGET,(APTR)&gImg, 0,
  153.             &gTxt, 0,(APTR)&gInf, gID, 0 },
  154.  
  155.    rGad = { &gGad, PRPX,PRPY, PRPW,PRPH,
  156.             GADGHNONE|GADGIMAGE|GRELBOTTOM,
  157.             GADGIMMEDIATE|RELVERIFY|FOLLOWMOUSE,
  158.             PROPGADGET,(APTR)&rImg, 0,
  159.             &rTxt, 0,(APTR)&rInf, rID, 0 };
  160.  
  161. /* Text Selection Gadgets */
  162. #define TXTX  8
  163. #define TXTY -17      /* Relative to bottom */
  164. #define TXTW  54
  165. #define TXTH  13
  166.  
  167. SHORT  txtBorXY[10] = {0,0, TXTW-1,0, TXTW-1,TXTH-1, 0,TXTH-1, 0,0};
  168. struct Border txtBor = { 0,0,3,0,JAM1,5,&txtBorXY[0],NULL };
  169.  
  170. struct IntuiText canTxt = {1,0,JAM1,4,3,0,"CANCEL",0};
  171. struct Gadget canGad = {
  172.    &rGad,   TXTX+122,TXTY,TXTW,TXTH, GADGHCOMP|GRELBOTTOM,
  173.    RELVERIFY, BOOLGADGET, (APTR)&txtBor, 0,
  174.    &canTxt, 0,0, canID, 0 };      /* Linked to red Prop Gadget */
  175.  
  176. struct IntuiText resTxt = {1,0,JAM1,8,3,0,"RESET",0};
  177. struct Gadget resGad = {
  178.    &canGad, TXTX+61,TXTY,TXTW,TXTH, GADGHCOMP|GRELBOTTOM,
  179.    RELVERIFY, BOOLGADGET, (APTR)&txtBor, 0,
  180.    &resTxt, 0,0, resID, 0 };
  181.  
  182. struct IntuiText okTxt = {1,0,JAM1,19,3,0,"OK",0};
  183. struct Gadget okGad = {
  184.    &resGad, TXTX,TXTY,TXTW,TXTH, GADGHCOMP|GRELBOTTOM,
  185.    RELVERIFY, BOOLGADGET, (APTR)&txtBor, 0,
  186.    &okTxt, 0,0, okID, 0 };  /* Color gads will be linked to this one */
  187.  
  188.  
  189. /* Palette Color Gadgets */
  190.  
  191. #define COLX  10     /* Offsets of first color gadget */
  192. #define COLY  14
  193. #define COLSW  4     /* Spacing between color gadgets */
  194. #define COLSH  4
  195. #define COLW8 18     /* Image width if 8 or more colors */
  196. #define COLW4 (COLW8+COLW8+COLSW)  /* 4 colors */
  197. #define COLW2 (COLW4+COLW4+COLSW)  /* 2 colors */
  198. #define COLH  10     /* Image height */
  199. #define COLDY (COLH+COLSH)
  200. SHORT   colW, colDX;
  201.  
  202. /* Drawn with DrawBorder() when color is selected */
  203. /* Note - the border coords will be filled in by initColGads() */
  204. SHORT  colBorXY[10] = {-2,-2, NULL,-2, NULL,COLH+1, -2,COLH+1, -2,-2};
  205. struct Border colBor = { 0,0,1,0,JAM1,5,&colBorXY[0],NULL };
  206.  
  207. SHORT colGadX[MAXCOLORS], colGadY[MAXCOLORS];
  208. struct Image  *colImgs;
  209. struct Gadget *colGads;
  210.  
  211.  
  212. /* Palette Window */ 
  213. struct NewWindow newpWin = {
  214.    36,24, PWIDE,NULL,   /* Height set before opening */
  215.    2,1,
  216.    MOUSEMOVE | GADGETUP | GADGETDOWN,             /* IDCMP Flags */
  217.    ACTIVATE | SMART_REFRESH | WINDOWDRAG | WINDOWDEPTH, /* Flags */
  218.    NULL,             /* First Gadget - set before OpenWindow */
  219.    NULL,             /* CheckMark */
  220.    "Palette Tool",   /* Title */
  221.    NULL,             /* Set up screen pointer before opening */
  222.    NULL,             /* No superbitmap */
  223.    0,0,0,0,          /* Max and Min (no sizing) */
  224.    NULL              /* Screen type - set before opening */
  225.    };
  226.  
  227.  
  228. struct Window   *pWin;      /* Palette window pointer */
  229. struct ViewPort *pVp;
  230. struct RastPort *pRp;
  231. struct IntuiMessage *pMsg;
  232. struct Gadget   *pMGad;
  233. int    pDepth, pColors, pReg, pTopSize;
  234. ULONG  pMClass, colGadMem, colGadMemSize;
  235. UWORD  curColor[MAXCOLORS], resColor[MAXCOLORS];
  236. USHORT pMGadID;
  237. BOOL Done;
  238.  
  239. /* 
  240.  * doPalette(screen)
  241.  */
  242.  
  243. doPalette(pScr)
  244. struct Screen *pScr;
  245.    {
  246.    int k;
  247.  
  248.    /* For safe pcleanup */
  249.    pWin    = NULL;
  250.    colGadMem = NULL;
  251.  
  252.    /* From the screen - needed by Allocations */
  253.    pVp = &pScr->ViewPort;
  254.    pDepth = pScr->RastPort.BitMap->Depth;
  255.    pColors = 1 << pDepth;
  256.  
  257.    /* Allocations - Note: Requires initialized pDepth, pColors ! */
  258.    colGadMemSize =
  259.       (sizeof(struct Gadget)<<pDepth)+(sizeof(struct Image)<<pDepth);
  260.    if(!(colGadMem=(ULONG)AllocMem(colGadMemSize,MEMF_PUBLIC|MEMF_CLEAR)))
  261.       {
  262.       pcleanup("Can't alloc gadget mem\n");
  263.       return(0);
  264.       }
  265.  
  266.    colGads = (struct Gadget *)colGadMem;
  267.    colImgs = (struct Image *)(colGadMem+(sizeof(struct Gadget)<<pDepth));
  268.  
  269.    /* Init Color Gadgets, linking last one to okGad */
  270.    initColGads(colGads,colImgs,pColors,&okGad);
  271.  
  272.    newpWin.FirstGadget = colGads;
  273.    newpWin.Screen = pScr;
  274.    newpWin.Type = pScr->Flags & 0x000f;
  275.    /* Color rows * color gad Y spacing + 10 for top border */
  276.    pTopSize = ((((pColors-1) >> 3)+1) * COLDY)+ 10;
  277.    newpWin.Height = PBASEHIGH + pTopSize;
  278.  
  279.    if (!(pWin = (struct Window *)OpenWindow(&newpWin)))
  280.       {
  281.       pcleanup("Can't open palette window\n");
  282.       return(0);
  283.       }
  284.    pRp = pWin->RPort;
  285.  
  286.    /* Get current colors and save for RESET */
  287.    for (k=0; k < pColors; k++)
  288.       curColor[k] = resColor[k] = GetRGB4(pVp->ColorMap,k);
  289.  
  290.    pReg = 0;  /* initially select color 0 */
  291.    selectColor(0, pReg);
  292.  
  293.    Done = FALSE;
  294.    while(! Done)
  295.       {
  296.       Wait(1<<pWin->UserPort->mp_SigBit);
  297.  
  298.       while (pMsg=(struct IntuiMessage *)GetMsg(pWin->UserPort))
  299.          {
  300.          /*  Note that rbg values are modified in 3 cases:
  301.           *     GADGETDOWN of the props
  302.           *     GADGETUP   of the props
  303.           *     MOUSEMOVE (reporting turned on by the props)
  304.           */
  305.  
  306.          pMClass  = pMsg->Class;
  307.          pMGad   = (struct Gadget *)pMsg->IAddress;
  308.          pMGadID = pMGad->GadgetID;
  309.  
  310.          ReplyMsg(pMsg);
  311.  
  312.          switch(pMClass)
  313.             {
  314.             case MOUSEMOVE:
  315.                modColor(pReg);
  316.                break;
  317.  
  318.             case GADGETDOWN:
  319.                switch(pMGadID)
  320.                   {
  321.                   case rID:  case gID:  case bID:
  322.                      modColor(pReg);
  323.                      break;
  324.  
  325.                   default:
  326.                      if ((pMGadID >= colID)&&(pMGadID<(colID+pColors)))
  327.                         {
  328.                         k = pReg;
  329.                         pReg = pMGadID - colID;
  330.                         selectColor(k,pReg);
  331.                         }
  332.                      break;
  333.                   }
  334.  
  335.             case GADGETUP:
  336.                switch (pMGadID)
  337.                   {
  338.                   case okID:        /* OK */
  339.                      Done = TRUE;
  340.                      break;
  341.  
  342.                   case resID:      /* RESET */
  343.                      resetColors();
  344.                      selectColor(pReg,pReg);
  345.                      break;
  346.  
  347.                   case canID:      /* CANCEL */
  348.                      resetColors();
  349.                      Done = TRUE;
  350.                      break;
  351.  
  352.                   case rID:  case gID:  case bID:
  353.                      modColor(pReg);
  354.                      break;
  355.  
  356.                   default:
  357.                      break;
  358.                   }
  359.  
  360.             default:
  361.                break;
  362.             }
  363.          }
  364.       }
  365.    pcleanup("");
  366.    return(1);
  367.    }
  368.  
  369.  
  370. pcleanup(s)
  371. char *s;
  372.    {
  373.    if (*s) printf(s);
  374.    if (pWin)
  375.       {
  376.       while (pMsg=(struct IntuiMessage *)GetMsg(pWin->UserPort))
  377.          {
  378.          ReplyMsg(pMsg);
  379.          }
  380.       CloseWindow(pWin);
  381.    if (colGadMem)  FreeMem(colGadMem,colGadMemSize);
  382.       }
  383.    }
  384.  
  385.  
  386. initColGads(gad,img,colors,lastLink)
  387. struct Gadget *gad;
  388. struct Image  *img;
  389. int colors;
  390. struct Gadget *lastLink;
  391.    {
  392.    struct Gadget *nextgad;
  393.    int k, lastk, row, col, rows;
  394.  
  395.    colW  = COLW8;
  396.    if (colors == 4)  colW = COLW4;
  397.    if (colors == 2)  colW = COLW2;
  398.    colDX = colW + COLSW; /* color width + space between colors */
  399.    colBorXY[2] = colW+1;
  400.    colBorXY[4] = colW+1;
  401.  
  402.    /*  Make array of colGad xy positions */
  403.    rows = ((colors-1) >> 3) + 1;  /* 8 colors per row */
  404.    for (row=0, k=0; row<rows; row++)
  405.       {
  406.       for (col=0; (col<8)&&(k<colors); col++, k++)
  407.          {
  408.          colGadX[k] = COLX + (col * colDX);
  409.          colGadY[k] = COLY + (row * COLDY);
  410.          }
  411.       }
  412.  
  413.    nextgad = gad+1;
  414.    lastk = colors-1;
  415.    for (k=0; k<colors; k++, gad++, img++, nextgad++)
  416.       {
  417.       if (k < lastk) gad->NextGadget = nextgad;
  418.       else gad->NextGadget = lastLink;
  419.       gad->LeftEdge = colGadX[k];
  420.       gad->TopEdge =  colGadY[k];
  421.       gad->Width =  colW;
  422.       gad->Height = COLH;
  423.       gad->Flags = GADGHBOX|GADGIMAGE;
  424.       gad->Activation = GADGIMMEDIATE;
  425.       gad->GadgetType = BOOLGADGET;
  426.       gad->GadgetRender = (APTR)img;
  427.       gad->GadgetID = colID + k;
  428.  
  429.       img->Width  = colW;
  430.       img->Height = COLH;
  431.       img->Depth  = 1;
  432.       img->PlaneOnOff = k;
  433.       }
  434.    }
  435.  
  436. resetColors()
  437.    {
  438.    int k; 
  439.    for(k=0; k<pColors; k++) curColor[k] = resColor[k];
  440.    LoadRGB4(pVp, &resColor[0], pColors);
  441.    }
  442.  
  443. selectColor(oldreg,newreg)
  444. int oldreg, newreg;
  445.    {
  446.    USHORT rgb;
  447.    int chx1,chy1,chx2,chy2;
  448.  
  449.    colBor.FrontPen = 0;
  450.    DrawBorder(pRp,&colBor,colGadX[oldreg],colGadY[oldreg]);
  451.    colBor.FrontPen = 1;
  452.    DrawBorder(pRp,&colBor,colGadX[newreg],colGadY[newreg]);
  453.  
  454.    /* Large chip of selected color */
  455.    chx1 = 8;  chy1 = pWin->Height - 63;
  456.    chx2 = 34; chy2 = pWin->Height - 23;
  457.  
  458.    SetDrMd(pRp,JAM1);
  459.    SetAPen(pRp,1);
  460.    Move(pRp,chx1,chy1);
  461.    Draw(pRp,chx2,chy1);
  462.    Draw(pRp,chx2,chy2);
  463.    Draw(pRp,chx1,chy2);
  464.    Draw(pRp,chx1,chy1);
  465.    SetAPen(pRp,newreg);
  466.    RectFill(pRp,chx1+2,chy1+2,chx2-2,chy2-2);
  467.  
  468.    rgb = curColor[newreg];
  469.    modColorProps(rgb);
  470.    printRGB((rgb>>8)&0xF, (rgb>>4)&0xF, rgb&0xF);
  471.    }
  472.  
  473.  
  474. modColor(reg)
  475. int reg;
  476.    {
  477.    USHORT r,g,b;
  478.  
  479.    r = (rInf.HorizPot >> 12) & 0x0F;
  480.    g = (gInf.HorizPot >> 12) & 0x0F;
  481.    b = (bInf.HorizPot >> 12) & 0x0F;
  482.  
  483.    curColor[reg] = (r << 8)|(g << 4)|b;
  484.  
  485.    LoadRGB4(pVp,&curColor[0],pColors);
  486.  
  487.    printRGB(r,g,b);
  488.    }
  489.  
  490.  
  491. modColorProps(rgb)
  492. USHORT rgb;
  493.    {
  494.    USHORT r,g,b;
  495.  
  496.    r = (rgb >> 8) & 0xF;
  497.    g = (rgb >> 4) & 0xF;
  498.    b = rgb & 0xF;
  499.    ModifyProp(&rGad,pWin,0,rInf.Flags,(r*0x11111111),
  500.               rInf.VertPot,rInf.HorizBody,rInf.VertBody);
  501.    ModifyProp(&gGad,pWin,0,gInf.Flags,(g*0x11111111),
  502.               gInf.VertPot,gInf.HorizBody,gInf.VertBody);
  503.    ModifyProp(&bGad,pWin,0,bInf.Flags,(b*0x11111111),
  504.               bInf.VertPot,bInf.HorizBody,bInf.VertBody);
  505.    }
  506.  
  507.  
  508. printRGB(r,g,b)
  509. USHORT r,g,b;
  510.    {
  511.    char str[4];
  512.    char *xstr;
  513.  
  514.    xstr = "0123456789ABCDEF";
  515.    str[0] = xstr[r];
  516.    str[1] = xstr[g];
  517.    str[2] = xstr[b];
  518.    SetAPen(pRp,1);
  519.    SetBPen(pRp,0);
  520.    SetDrMd(pRp,JAM2);
  521.    Move(pRp,PWIDE-40,pWin->Height - 40);
  522.    Text(pRp,&str[0],3);
  523.    }
  524.